﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
public partial class TinNhan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int getCount(DataTable dem)
    {
        int d = 0;
        foreach (DataRow row in dem.Rows)
        {
            d = int.Parse(row["dem"].ToString());
        }
        return d;
    }
    public void GoiTinNhan(object sender, EventArgs e)
    {
        try
        {
            string nguoinhan = txtNguoinhan.Text;
            string tieude = txtTieuDeTN.Text;
            string noidung = txtNoiDungTN.Text;
            string username = Session["user"].ToString();
            string sqlCount = " select count(*) as dem from tinnhan";
            DataTable dem = Database.GetData(sqlCount);
            int d = this.getCount(dem);

            string sql = " insert into tinnhan (matinnhan,username,tieude,nguoinhan,ngaygoi,noidung) values(@matinnhan,@username,@tieude,@nguoinhan,@ngaygoi,@noidung)";

            Database.ExecuteNonQuery(sql, "@matinnhan", "TN" + (d + 1), "@username", username, "@tieude", tieude, "@nguoinhan", nguoinhan, "@ngaygoi", DateTime.Now, "@noidung", noidung);
        }
        catch (Exception err)
        {
            throw err;
        }
    }
}